package cn.cloudreal.cbms.system.action;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.system.service.OrganizaService;
import cn.cloudreal.cbms.system.service.RoleService;
import cn.cloudreal.cbms.system.service.UserInfoService;
import cn.cloudreal.cbms.vo.Organiza;
import cn.cloudreal.cbms.vo.Role;
import cn.cloudreal.cbms.vo.UserInfo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * <p>
 * Title: CBMS
 * </p>
 * <p>
 * Description:用户管理查询数据服务层视图类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author Gaoxuejiao
 * @date 2012-07-17
 */
public class UserInfoAction extends ActionSupport
{
    /** serialVersionUID */
    private static final long serialVersionUID = 1L;

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(UserInfoAction.class);

    /**
     * 注入service 类
     */
    private UserInfoService userInfoService;

    /**
     * 注入service 类
     */
    private OrganizaService organizaService;

    /**
     * 注入service 类
     */
    private RoleService roleService;

    /** 角色列表 */
    private List<Role> roleLists;

    /** 组织架构列表 */
    private List<Organiza> organizaLists;

    /** id */
    long id;

    /** 当前页 */
    int currentPage;

    /** 用户列表 */
    private List<UserInfo> userInfoList;

    /**
     * 用户实体信息
     */
    private UserInfo userInfo;

    /** 分页对象 */
    PageBean page;

    /**
     * 返回树形菜单字符串
     */
    String flag;

    /**
     * 操作返回类型
     */
    private String type;

    /**
     * 列表
     * 
     * @return String
     */
    public String list()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction list() method,return String");
        }
        try
        {
            userInfoList = new ArrayList<UserInfo>();

            // 获取当前session对象中所属城市
            UserInfo sessionUser = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            if (null == userInfo)
            {
                userInfo = new UserInfo();
            }
            // 添加查询城市条件
            userInfo.setUsercity(sessionUser.getUsercity());
            // 实例化 page分页对象
            page = new PageBean(currentPage == 0 ? 1 : currentPage,
                    Constants.PAGE_SIZE);

            // 获取数据
            userInfoList = userInfoService.getUserInfos(userInfo, page);
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常信息 返回error
            log.error("Error UserInfoAction list() method,return String", e);
            return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit UserInfoAction list() method,return String");
        }
        return SUCCESS;
    }

    /**
     * 查看
     * 
     * @return
     */
    public String detail()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction view() method,return String");
        }
        try
        {
            // 如果不为空
            if (null != userInfo)
            {

                userInfo = userInfoService.getUserInfo(userInfo.getId());
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志
            log.error("Error UserInfoAction view() method", e);
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 跳转到修改页面
     * 
     * @return SUCCESS
     */
    public String toUserInfoEdit()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction toUserInfoEdit() method,return String");
        }
        try
        {
            // 如果不为空
            if (null != userInfo)
            {
                // 获取当前session对象中所属城市
                UserInfo userSession = (UserInfo) ServletActionContext.getRequest()
                        .getSession()
                        .getAttribute(Constants.SESSIONPRKEY);

                userInfo = userInfoService.getUserInfo(userInfo.getId());
                // 查询所有角色信息
                roleLists = new ArrayList<Role>();

                Role conditionRole = new Role();
                conditionRole.setCity(userSession.getUsercity());
                // 获取所有角色对象信息
                roleLists = roleService.getAllRoles(conditionRole);

                // 查询组织架构信息 获取当前节点集合
                organizaLists = new ArrayList<Organiza>();

                // 非空判断 当前用户城市编码是否为空
                if (null != userSession && null != userSession.getUsercity())
                {
                    organizaLists = organizaService.getOrganizaByCity(userSession.getUsercity());
                }
                else
                {
                    organizaLists = organizaService.getOrganizaByCity("DG");
                }

                // 获取生成组织架构字符串
                flag = Constants.getOrganizaTree(organizaLists);
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志
            log.error("Error UserInfoAction toUserInfoEdit() method", e);
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 执行修改页面
     * 
     * @return SUCCESS
     */
    public String doUserInfoEdit()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction doUserInfoEdit() method,return String");
        }
        // response 对象
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try
        {
            // 获取PrintWriter对象
            ServletActionContext.getRequest()
                    .setCharacterEncoding(Constants.STR_UTF);
            response.setContentType(Constants.STR_CHARSET);
            out = response.getWriter();
            // 如果不为空
            if (null != userInfo)
            { // 默认当前登录人地市
                UserInfo userSession = (UserInfo) ServletActionContext.getRequest()
                        .getSession()
                        .getAttribute("logUser");
                userInfo.setUsercity(userSession.getUsercity());
                userInfoService.updateUserInfo(userInfo);
            }
            out.print("true");
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志
            log.error("Error RoleAction doUserInfoEdit() method,return String ",
                    e);
            out.print("error");
            out.flush();
            out.close();
        }
        return null;
    }

    /**
     * 跳转到新增页面
     * 
     * @return SUCCESS
     */
    public String toUserInfoAdd()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction toUserInfoAdd() method");
        }
        try
        {

            // 获取当前session对象中所属城市
            UserInfo userSession = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            // 查询所有角色信息
            roleLists = new ArrayList<Role>();

            Role conditionRole = new Role();
            conditionRole.setCity(userSession.getUsercity());
            // 获取所有角色对象信息
            roleLists = roleService.getAllRoles(conditionRole);

            // 查询组织架构信息 获取当前节点集合
            organizaLists = new ArrayList<Organiza>();

            // 非空判断 当前用户城市编码是否为空
            if (null != userSession && null != userSession.getUsercity())
            {
                organizaLists = organizaService.getOrganizaByCity(userSession.getUsercity());
            }
            else
            {
                organizaLists = organizaService.getOrganizaByCity("DG");
            }

            // 获取生成组织架构字符串
            flag = Constants.getOrganizaTree(organizaLists);
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志
            log.error("Error UserInfoAction toUserInfoAdd() method", e);
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 执行添加操作
     * 
     * @return
     */
    public String doUserInfoAdd()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction doUserInfoAdd() method");
        }

        // response 对象
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try
        {
            // 获取PrintWriter对象
            ServletActionContext.getRequest()
                    .setCharacterEncoding(Constants.STR_UTF);
            response.setContentType(Constants.STR_CHARSET);
            out = response.getWriter();
            if (null != userInfo)
            {
                // 默认当前登录人地市
                UserInfo userSession = (UserInfo) ServletActionContext.getRequest()
                        .getSession()
                        .getAttribute("logUser");
                userInfo.setUsercity(userSession.getUsercity());

                // 执行添加操作
                userInfoService.addUserInfo(userInfo);
            }
            out.print("true");
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志
            log.error("Error RoleAction doUserInfoEdit() method,return String ",
                    e);
            out.print("error");
            out.flush();
            out.close();
        }

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit UserInfoAction doUserInfoAdd() method");
        }
        return null;
    }

    /**
     * 删除
     * 
     * @return SUCCESS
     */
    public String delete()
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction delete() method,return String");
        }
        // response 对象
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try
        {
            if (null != userInfo)
            {
                // 获取PrintWriter对象
                ServletActionContext.getRequest()
                        .setCharacterEncoding(Constants.STR_UTF);
                response.setContentType(Constants.STR_CHARSET);
                out = response.getWriter();

                // 判断是否为空，不为空则转换
                if (null != userInfo.getUserCode())
                {
                    String userCode = URLDecoder.decode(userInfo.getUserCode(),
                            "UTF-8");
                    userInfo.setUserCode(userCode);
                }
                userInfoService.delUserInfo(userInfo);
                out.print("success");
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception 添加异常日志 返回Error
            log.error("Error UserInfoAction delete() method,return String", e);
            // return ERROR;
            out.print("error");
            out.flush();
            out.close();
            return null;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit UserInfoAction delete() method,return String ");
        }
        return null;
    }

    /**
     * 判断用户账号是否已经存在
     * 
     * @return
     */
    public String isExistUserCode()
    {

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction isExistUserCode() method,return String");
        }
        // response 对象
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try
        {
            // 获取PrintWriter对象
            ServletActionContext.getRequest()
                    .setCharacterEncoding(Constants.STR_UTF);
            response.setContentType(Constants.STR_CHARSET);
            out = response.getWriter();
            String userCode = URLDecoder.decode(userInfo.getUserCode(), "UTF-8");

            userInfo.setUserCode(userCode);
            // 获取返回值
            boolean flagStr = userInfoService.isExistUserCode(userInfo);

            out.print(flagStr);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            // 打印方法体异常日志
            log.error("Error UserInfoAction isExistUserCode() method,return String ",
                    e);
            // return ERROR;
            out.print("error");
            out.flush();
            out.close();
            return null;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit UserInfoAction isExistUserCode() method,return String ");
        }
        return null;
    }

    /**
     * 判断用户名称是否已经村子啊
     * 
     * @return
     */
    public String isExistUserName()
    {

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter UserInfoAction isExistUserCode() method,return String");
        }
        // response 对象
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try
        {
            // 获取PrintWriter对象
            ServletActionContext.getRequest()
                    .setCharacterEncoding(Constants.STR_UTF);
            response.setContentType(Constants.STR_CHARSET);
            out = response.getWriter();
            String userName = URLDecoder.decode(userInfo.getUserName(), "UTF-8");

            userInfo.setUserName(userName);

            // 默认当前登录人地市
            UserInfo userSession = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute("logUser");
            userInfo.setUsercity(userSession.getUsercity());

            // 获取返回值
            boolean flagStr = userInfoService.isExistUserCode(userInfo);

            out.print(flagStr);
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
            // 打印方法体异常日志
            log.error("Error UserInfoAction isExistUserCode() method,return String ",
                    e);
            out.print("error");
            out.flush();
            out.close();
            // return ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit UserInfoAction isExistUserCode() method,return String ");
        }
        return null;

    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getFlag()
    {
        return flag;
    }

    public void setFlag(String flag)
    {
        this.flag = flag;
    }

    public PageBean getPage()
    {
        return page;
    }

    public void setPage(PageBean page)
    {
        this.page = page;
    }

    public UserInfoService getUserInfoService()
    {
        return userInfoService;
    }

    public void setUserInfoService(UserInfoService userInfoService)
    {
        this.userInfoService = userInfoService;
    }

    public List<UserInfo> getUserInfoList()
    {
        return userInfoList;
    }

    public void setUserInfoList(List<UserInfo> userInfoList)
    {
        this.userInfoList = userInfoList;
    }

    public UserInfo getUserInfo()
    {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo)
    {
        this.userInfo = userInfo;
    }

    public RoleService getRoleService()
    {
        return roleService;
    }

    public void setRoleService(RoleService roleService)
    {
        this.roleService = roleService;
    }

    public OrganizaService getOrganizaService()
    {
        return organizaService;
    }

    public void setOrganizaService(OrganizaService organizaService)
    {
        this.organizaService = organizaService;
    }

    public List<Role> getRoleLists()
    {
        return roleLists;
    }

    public void setRoleLists(List<Role> roleLists)
    {
        this.roleLists = roleLists;
    }

    public List<Organiza> getOrganizaLists()
    {
        return organizaLists;
    }

    public void setOrganizaLists(List<Organiza> organizaLists)
    {
        this.organizaLists = organizaLists;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

}
